package dhbw.GUI.Panels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListModel;
import javax.swing.table.DefaultTableCellRenderer;

import dhbw.Data.Language;
import dhbw.Data.Pruefung;
import dhbw.Data.Settings;
import dhbw.Data.Vorlesung;
import dhbw.GUI.TableModels.EntryStudentLeistungsuebersicht;
import dhbw.GUI.TableModels.TableModelStudent;
import dhbw.GUI.TableModels.TableRendererNoten;
import dhbw.Roles.Student;
import Main.MyController;

public class PanelStudentGesamtuebersicht extends JPanel {

	private MyController myc;
	private JTable table;
	private JLabel durchschnitt;

	public PanelStudentGesamtuebersicht(MyController myc) {
		this.myc = myc;
		build();
	}

	/**
	 * Build Panel
	 */
	private void build() {
		this.setBackground(Settings.BG_COLOR);
		table = new JTable();
		this.setLayout(new BorderLayout());

		TableModelStudent model = new TableModelStudent(this.myc,
				getModelContentList());
		table.setModel(model);
		DefaultTableCellRenderer ren = new TableRendererNoten();
		table.setDefaultRenderer(Object.class, ren);
		table.getColumnModel().getColumn(0).setPreferredWidth(500);
		table.setFocusable(false);
		table.setShowVerticalLines(true);
		table.setShowHorizontalLines(true);
		table.setRowHeight(30);
		table.setAutoCreateRowSorter(true);
		durchschnitt = new JLabel("---");

		durchschnitt.setText("<html><br>"
				+ Language.AVERAGE[this.myc.getLanguage()] + getSchnitt()
				+ "                    <br><br></html>");
		this.add("North", durchschnitt);
		this.add("Center", new JScrollPane(table));
	}

	/**
	 * Get a List with all lectures and its grades
	 * 
	 * @return ArrayList {@link EntryStudentLeistungsuebersicht}
	 */
	private ArrayList<EntryStudentLeistungsuebersicht> getModelContentList() {
		ArrayList<EntryStudentLeistungsuebersicht> mainlist = new ArrayList<EntryStudentLeistungsuebersicht>();
		HashMap<Vorlesung, ArrayList<Pruefung>> exam = ((Student) this.myc
				.getActiveUser()).getExamResults();

		for (Vorlesung vl : exam.keySet()) {
			if (exam.get(vl).size() > 0) {
				Pruefung p1 = ((ArrayList<Pruefung>) exam.get(vl)).get(exam
						.get(vl).size() - 1);
				mainlist.add(new EntryStudentLeistungsuebersicht(vl
						.getBezeichnung(), p1.getErgebniss(), myc
						.getNotenspiegel().getNote(p1.getErgebniss())));
			}
		}
		return mainlist;
	}

	/**
	 * Get Average from actual model
	 * 
	 * @return Returns the Average of the actual Model
	 */
	private int getSchnitt() {
		int rtn = 0;
		int count = 0;
		try {
			for (int i = 0; i < table.getModel().getRowCount(); i++) {
				count++;
				rtn = rtn
						+ Integer.parseInt(table.getModel().getValueAt(i, 2)
								.toString());
			}
			rtn = Math.round(rtn / count);
		} catch (Exception ex) {
		}
		return rtn;
	}
}
